package com.ceshi.util.Jenkins;

import com.offbytwo.jenkins.JenkinsServer;
import com.offbytwo.jenkins.client.JenkinsHttpClient;
import com.offbytwo.jenkins.model.Build;
import com.offbytwo.jenkins.model.Job;
import com.offbytwo.jenkins.model.JobWithDetails;
import com.offbytwo.jenkins.model.MavenJobWithDetails;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * Job(任务) 相关操作
 * <p>
 * 例如对任务的增、删、改、查等操作
 */
public class JobApi {

    // Jenkins 对象
    private JenkinsServer jenkinsServer;
    // http 客户端对象
    private JenkinsHttpClient jenkinsHttpClient;

    /**
     * 构造方法中调用连接 Jenkins 方法
     */
    public JobApi() {
        JenkinsApi jenkinsApi = new JenkinsApi();
        // 连接 Jenkins
        jenkinsServer = JenkinsConnect.connection();
        // 设置客户端连接 Jenkins
        jenkinsHttpClient = JenkinsConnect.getClient();
    }

    /**
     * 创建 Job
     */
    public void ceateJob(String jobName, String jobXml) {
        try {
            jenkinsServer.createJob(jobName, jobXml, true);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 更新 Job
     * <p>
     * 更改之前创建的无参数Job，更改其为参数Job
     */
    public void updateJob(String jobName, String jobXml) {
        try {
            jenkinsServer.updateJob(jobName, jobXml,true);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取 Job 基本信息
     */
    public void getJob(String jobName) {
        try {
            // 获取 Job 信息
            JobWithDetails job = jenkinsServer.getJob(jobName);
            // 获取 Job 名称
            System.out.println(job.getName());
            // 获取 Job URL
            System.out.println(job.getUrl());
            // 获取 Job 下一个 build 编号
            System.out.println(job.getNextBuildNumber());
            // 获取 Job 显示的名称
            System.out.println(job.getDisplayName());
            // 输出 Job 描述信息
            System.out.println(job.getDescription());
            // 获取 Job 下游任务列表
            System.out.println(job.getDownstreamProjects());
            // 获取 Job 上游任务列表
            System.out.println(job.getUpstreamProjects());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取 Maven Job 信息
     */
    public void getMavenJob(String jobName) {
        try {
            // 获取 Job 信息
            MavenJobWithDetails job = jenkinsServer.getMavenJob(jobName);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取 Job 列表
     */
    public void getJobList() {
        try {
            Map<String, Job> jobs = jenkinsServer.getJobs();
            for (Job job : jobs.values()) {
                System.out.println(job.getName());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取 View 名称获取 Job 列表
     */
    public void getJobListByView(String view) {
        try {
            Map<String, Job> jobs = jenkinsServer.getJobs(view);
            for (Job job : jobs.values()) {
                System.out.println(job.getName());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 查看 Job XML 信息
     */
    public void getJobConfig(String jobName) {
        try {
            String xml = jenkinsServer.getJobXml(jobName);
            System.out.println(xml);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 执行无参数 Job build
     */
    public void buildJob(String jobName) {
        try {
            jenkinsServer.getJob(jobName).build(true);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 执行带参数 Job build
     */
    public void buildParamJob(String jobName) {
        try {
            /**
             * 例如，现有一个job，拥有一个字符参数"key"
             * 现在对这个值进行设置，然后执行一个输出这个值的脚本
             */
            // 设置参数值
            Map<String, String> param = new HashMap<String, String>();
            param.put("key", "hello world!");
            // 执行 build 任务
            jenkinsServer.getJob(jobName).build(param);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 停止最后构建的 Job Build
     */
    public void stopLastJobBuild(String jobName) {
        try {
            // 获取最后的 build 信息
            Build build = jenkinsServer.getJob(jobName).getLastBuild();
            // 停止最后的 build
            build.Stop();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 删除 Job
     */
    public void deleteJob(String jobName, boolean crumbFlag) {
        try {
            jenkinsServer.deleteJob(jobName, crumbFlag);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 禁用 Job
     */
    public void disableJob(String jobName) {
        try {
            jenkinsServer.disableJob(jobName);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 启用 Job
     */
    public void enableJob(String jobName, boolean crumbFlag) {
        try {
            jenkinsServer.enableJob(jobName, crumbFlag);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


}